<?php

class SimditorAdmin extends AdminBase
{
	private $nohf = ['saveimg'];
	private $mysql = null;

	public function __construct()
	{
		parent::__construct();
		if(!in_array(N_ACTION,$this->nohf)){
			n_mod('skin')->header('admin');
		}
		$this->mysql = n_api('mysql')->cname('weixin');
	}

	public function saveImg()
	{
		if ($_FILES['upload_file']['error'] != UPLOAD_ERR_OK) {
			switch($_FILES['upload_file']['error']) {
				//其值为 1，上传的文件超过了 php.ini 中 upload_max_filesize 选项限制的值
				case UPLOAD_ERR_INI_SIZE:
				die('The upload file exceeds the upload_max_filesize directive in php.ini');
				break;

				//其值为 2，上传文件的大小超过了 HTML 表单中 MAX_FILE_SIZE 选项指定的值
				case UPLOAD_ERR_FORM_SIZE:
				die('The uploaded file exceeds the MAX_FILE_SIZE directive that was specified in the HTML form.');
				break;

				//其值为 3，文件只有部分被上传
				case UPLOAD_ERR_PARTIAL:
				die('The uploaded file was only partially uploaded.');
				break;

				//其值为 4，没有文件被上传
				case UPLOAD_ERR_NO_FILE:
				die('No file was uploaded.');
				break;

				//其值为 6，找不到临时文件夹
				case UPLOAD_ERR_NO_TMP_DIR:
				die('The server is missing a temporary folder.');
				break;

				//其值为 7，文件写入失败
				case UPLOAD_ERR_CANT_WRITE:
				die('The server failed to write the uploaded file to disk.');
				break;

				//其他异常
				case UPLOAD_ERR_EXTENSION:
				die('File upload stopped by extension.');
				break;
    		}
		}

		$original_filename = $_FILES['upload_file']['name'];
		$type = $_FILES['upload_file']['type'];
		$time = time();
		$data = [
			'original_filename' => $original_filename,
			'ctime' => $time
		];
		$id = $this->mysql->insert('simditor_main', $data);

		if (!is_int($id))
			die('error happened');

		switch ($type) {
			case 'image/gif':
			$image = imagecreatefromgif($_FILES['upload_file']['tmp_name']) or die('The file you upload was not supported filetype');
			$imageName = md5($id.'fuckfina'.$time).'.gif';
			break;

			case 'image/jpeg':
			$image = imagecreatefromjpeg($_FILES['upload_file']['tmp_name']) or die('The file you upload was not supported filetype');
			$imageName = md5($id.'fuckfina'.$time).'.jpg';
			break;

			case 'image/png':
			$image = imagecreatefrompng($_FILES['upload_file']['tmp_name']) or die('The file you upload was not supported filetype');
			$imageName = md5($id.'fuckfina'.$time).'.png';
			break;

			default:
			die('The file you uploaded was not a supported filetype.');
		}

		$baseDir = N_PLUGINS . '/simditor/img/';
		$imgPath = $baseDir . $imageName;
		$query = "update simditor_main set img_name = '$imageName' where id = $id";
		$this->mysql->exec($query);

		switch ($type) {
			case 'image/gif':
			imagegif($image,$imgPath);
			break;

			case 'image/jpeg':
			imagejpeg($image,$imgPath);
			break;

			case 'image/png':
			imagepng($image,$imgPath);
			break;

			default:
			die('The file you uploaded was not a supported filetype.');
		}

		imagedestroy($image);

		$rs = ['success'=>true, 'file_path'=>n_img('simditor', $imageName)];
		echo json_encode($rs);
	}
}